package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 *
 * @author axi
 *
 */
public class Tuan800BbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bbs.tuan800.com/.*/topics/[0-9]+"
				//http://bbs.tuan800.com/sheying/topics/1108214
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if(!isDetailPage(url)){
		}
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta bbs = new NewsMeta();
		String author = null;
		String title = "";
		String content = "";
		Long date = null;
		Integer commentNum = 0;
		Integer clickNum = 0;

		/**
		 * 标题
		 */
		title = doc.select("h2").text();
		/**
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select("div.post_time").text());
		/**
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.content-c"));
		 Element first = doc.select("div.user_info a[href*=http://www.tuan800.com/user/]").first();
		 if (first!=null){
			 author = first.text();
		 }
		String commTxt = doc.select("div.forum_title").text();
		commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "点击"));
		clickNum = NumberUtils.matchNumber(StringUtils.substringAfterLast(commTxt, " /"));
		if(StringUtils.isBlank(title)){
		}
		bbs.setUrl(url);
		bbs.setTitle(title);

		bbs.setAuthor(author);
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setType(4);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	/**
	 * @param args
	 */
	

	
}
